<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<script src="/static/admin/js/math.js"></script>
<style>
    body{background-color: #FFFFFF}
    .change_page{background-color: #FFFFFF}
    td{padding: 0!important;}
    .xuan{text-align: center;width: 45px!important;}
    .xuanname{padding-left: 10px}
    #list_list .buttonnone{position: absolute;right: 0;padding: 0 6px!important;top: -1px;color: #707070;background-color: #fff;border: 1px solid #e6e6e6;}
    #list_list .buttonlv{position: absolute;right: 0;padding: 0 6px!important;top: -1px;background-color: #248BFE}
    #qing-tbody td{padding-left: 15px !important;}
    #other-tbody td{padding-left: 15px !important;}
</style>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span id="flowsetname">选择分部项</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>选择分部项</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th style="width: 45px"><input type="checkbox" class="xuanhang" lay-filter="xuanhang" lay-skin="primary"></th>
                            <th>分部行名称</th>
                            <th>人工支付比例(留空默认为0)</th>
                            <th>材料支付比例(留空默认为0)</th>
                            <th>机械支付比例(留空默认为0)</th>
                            <th>分包支付比例(留空默认为0)</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height: 500px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<div id="qing-div" style="display: none;">
    <table class="layui-table">
        <thead>
        <tr>
            <th>名称</th>
            <th>分项名称</th>
            <th>累计人工</th>
            <th>累计材料</th>
            <th>累计机械</th>
            <th>累计分包</th>
            <th>合计产值</th>
        </tr>
        </thead>
        <tbody id="qing-tbody">
        </tbody>
    </table>
</div>
<script>


    var CurrentFennameArr = parent.CurrentFennameArr;
    var CurrentCheckObj = parent.CurrentCheckObj;
    var parent_qingid = getQueryVariable('qingid');
    var param_enginid = getQueryVariable('enginid');

    function openChan(type,id){
        facade.ajax({type: 'get', url: ask_main_huizi_search,data:{enginid:param_enginid,key:hang_key,type:type,id:id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                var str = '';
                if(type == 'qing'){
                    str += '<tr>';
                    str += '<td rowspan="'+(data.list.length + 1)+'">'+data.name+'</td>';
                    str += '<td rowspan="'+(data.list.length + 1)+'">'+data.moeny+'</td>';
                    str += '</tr>';
                    var list = data.list;
                    for(var j in list){
                        str += '<tr>';
                        str += '<td>'+list[j].name+'</td>';
                        str += '<td>'+list[j].ren+'</td>';
                        str += '<td>'+list[j].cai+'</td>';
                        str += '<td>'+list[j].ji+'</td>';
                        str += '<td>'+list[j].fen+'</td>';
                        str += '<td>'+list[j].money+'</td>';
                        str += '</tr>';
                    }
                    $('#qing-tbody').html(str);

                    layer.open({
                        type: 1,
                        area:['90%','80%'],
                        content: $('#qing-div')
                    });
                }else{
                    str += '<tr>';
                    str += '<td>'+data.name+'</td>';
                    str += '<td>'+data.money+'</td>';
                    str += '</tr>';
                    $('#other-tbody').html(str);
                    layer.open({
                        type: 1,
                        area:['90%','80%'],
                        content: $('#other-div')
                    });
                }

                render_from();
            }
        });
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        var existObj;
        var existStr =  $("#list_list .fenname_"+parent_qingid , parent.document).text() || '';
        if(existStr){
            try{
                existObj = JSON.parse(existStr);
            }catch (e) {
                existObj = '';
            }
        }else{
            existObj = [];
        }

        var str = '';
        for (var key in CurrentFennameArr) {
            var isCun = is_in_arr(CurrentFennameArr[key], existObj);


            if(isCun.fenname == CurrentFennameArr[key]){
                str += '<tr>';
                str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
                if(CurrentCheckObj && $.inArray(CurrentFennameArr[key],CurrentCheckObj) != -1){
                    str += '<td><input type="text" readonly class="layui-input fenname colorlan" onclick="openChan(\''+CurrentFennameArr[key]+'\')" value="'+CurrentFennameArr[key]+'"></td>';
                }else{
                    str += '<td><input type="text" readonly class="layui-input fenname" value="'+CurrentFennameArr[key]+'"></td>';
                }


                if(isCun.rate_ren_val == 0 || isCun.rate_ren_val == '' || isCun.rate_ren_val == undefined){
                    str += '<td><input type="text" class="layui-input rate_ren" value=""></td>';
                }else{
                    str += '<td><input type="text" class="layui-input rate_ren" value="'+isCun.rate_ren_val+'"></td>';
                }

                if(isCun.rate_cai_val == 0 || isCun.rate_cai_val == '' || isCun.rate_cai_val == undefined){
                    str += '<td><input type="text" class="layui-input rate_cai" value=""></td>';
                }else{
                    str += '<td><input type="text" class="layui-input rate_cai" value="'+isCun.rate_cai_val+'"></td>';
                }

                if(isCun.rate_ji_val == 0 || isCun.rate_ji_val == '' || isCun.rate_ji_val == undefined){
                    str += '<td><input type="text" class="layui-input rate_ji" value=""></td>';
                }else{
                    str += '<td><input type="text" class="layui-input rate_ji" value="'+isCun.rate_ji_val+'"></td>';
                }

                if(isCun.rate_fen_val == 0 || isCun.rate_fen_val == '' || isCun.rate_fen_val == undefined ){
                    str += '<td><input type="text" class="layui-input rate_fen" value=""></td>';
                }else{
                    str += '<td><input type="text" class="layui-input rate_fen" value="'+isCun.rate_fen_val+'"></td>';
                }
                str += '</tr>';
            }else{
                str += '<tr>';
                if(CurrentCheckObj && $.inArray(CurrentFennameArr[key],CurrentCheckObj) != -1){
                    str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
                    str += '<td><input type="text" readonly class="layui-input fenname colorlan" onclick="openChan(\''+CurrentFennameArr[key]+'\')" value="'+CurrentFennameArr[key]+'"></td>';
                }else{
                    str += '<td class="xuan" title="该项在产值中未设置"><input type="checkbox" class="xuanhang" lay-skin="primary" disabled></td>';
                    str += '<td><input type="text" readonly class="layui-input fenname" value="'+CurrentFennameArr[key]+'"></td>';
                }
                str += '<td><input type="text" class="layui-input rate_ren" value=""></td>';
                str += '<td><input type="text" class="layui-input rate_cai" value=""></td>';
                str += '<td><input type="text" class="layui-input rate_ji" value=""></td>';
                str += '<td><input type="text" class="layui-input rate_fen" value=""></td>';
                str += '</tr>';
            }
        }
        $("#list_list").html(str);
        render_from();

        form.on('checkbox(xuanhang)',function (data) {
            if($(data.elem).prop("checked")){
                $('#list_list .xuanhang').prop("checked", true);
            }else{
                $('#list_list .xuanhang').prop("checked", false);
            }
            render_from();
        });



        form.on('submit(add)', function(data){
            var checkStr = '';
            var dataArr = [];

            $('#list_list tr').each(function (i) {
                var checked = $(this).find('.xuanhang').prop("checked");
                var fenname = $(this).find('.fenname').val() || '';
                var rate_ren_val = $(this).find('.rate_ren').val() || '';
                if(rate_ren_val == '='){rate_ren_val = '';}
                var rate_cai_val = $(this).find('.rate_cai').val() || '';
                if(rate_cai_val == '='){rate_cai_val = '';}
                var rate_ji_val = $(this).find('.rate_ji').val() || '';
                if(rate_ji_val == '='){rate_ji_val = '';}
                var rate_fen_val = $(this).find('.rate_fen').val() || '';
                if(rate_fen_val == '='){rate_fen_val = '';}

                var rate_ren = '';
                if(rate_ren_val != ''){
                    var val = rate_ren_val.split('=')[1];
                    if(/=/.test(val)){
                        layer.msg('第'+(i+1)+'行，人工费错误');
                        rate_ren = '';
                    }else{
                        rate_ren = math.evaluate(val).toFixed(6);
                    }
                }
                var rate_cai = '';
                if(rate_cai_val != ''){
                    var val = rate_cai_val.split('=')[1];
                    if(/=/.test(val)){
                        layer.msg('第'+(i+1)+'行，人工费错误');
                        rate_cai = '';
                    }else{
                        rate_cai = math.evaluate(val).toFixed(6);
                    }
                }
                var rate_ji = '';
                if(rate_ji_val != ''){
                    var val = rate_ji_val.split('=')[1];
                    if(/=/.test(val)){
                        layer.msg('第'+(i+1)+'行，人工费错误');
                        rate_ji = '';
                    }else{
                        rate_ji = math.evaluate(val).toFixed(6);
                    }
                }
                var rate_fen = '';
                if(rate_fen_val != ''){
                    var val = rate_fen_val.split('=')[1];
                    if(/=/.test(val)){
                        layer.msg('第'+(i+1)+'行，人工费错误');
                        rate_fen = '';
                    }else{
                        rate_fen = math.evaluate(val).toFixed(6);
                    }
                }

                if(checked){
                    dataArr.push({
                        fenname: fenname,
                        rate_ren: rate_ren,
                        rate_cai: rate_cai,
                        rate_ji: rate_ji,
                        rate_fen: rate_fen,
                        rate_ren_val:rate_ren_val,
                        rate_cai_val:rate_cai_val,
                        rate_ji_val:rate_ji_val,
                        rate_fen_val:rate_fen_val,
                    });
                }
            });


            var datastr = JSON.stringify(dataArr);
            if(dataArr.length > 0){
                $("#list_list .fennamebut_"+parent_qingid , parent.document).removeClass('buttonnone').addClass('buttonlv');
            }else{
                $("#list_list .fennamebut_"+parent_qingid , parent.document).removeClass('buttonlv').addClass('buttonnone');
            }

            $("#list_list .fenname_"+parent_qingid , parent.document).text(datastr);

            layer_close();
            return false;
        });

    });


    $('#list_list').on('focus','.rate_ren,.rate_cai,.rate_ji,.rate_fen',function () {
        var val = $(this).val();
        if(val == ''){
            $(this).val('=');
        }
    });
    $('#list_list').on('blur','.rate_ren,.rate_cai,.rate_ji,.rate_fen',function () {
        var val = $(this).val();
        if(val == '='){
            $(this).val('');
        }
    });


    function is_in_arr(name, arr) {
        var data = {
            fenname: '',
            rate_ren: '',
            rate_cai: '',
            rate_ji: '',
            rate_fen: '',
        };
        if(!arr || arr.length < 1){
            return data;
        }
        for (var key in arr) {
            if(arr[key].fenname == name){
                data.fenname = arr[key].fenname;
                data.rate_ren = arr[key].rate_ren;
                data.rate_cai = arr[key].rate_cai;
                data.rate_ji = arr[key].rate_ji;
                data.rate_fen = arr[key].rate_fen;

                data.rate_ren_val = arr[key].rate_ren_val;
                data.rate_cai_val = arr[key].rate_cai_val;
                data.rate_ji_val = arr[key].rate_ji_val;
                data.rate_fen_val = arr[key].rate_fen_val;
            }
        }

        return data;
    }
</script>
</body>
</html>
